package com.example.test_load_pic_rough;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.cherry.lib.doc.office.fc.hssf.formula.function.T;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class CutVideoActivity extends AppCompatActivity {

    private MediaMetadataRetriever retriever;
    String filePath ="/storage/emulated/0/a_test_f/vi/video041402.mp4" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cut_video);

        init();
    }


    private void init(){
        TextView t  = findViewById(R.id.tv_filepath);
        t.setText(filePath);

        ImageView iv = findViewById(R.id.iv);
        File file = new File(filePath);
        if (file.exists()){
            new Thread(()->{
                retriever = new MediaMetadataRetriever();
                retriever.setDataSource(filePath);
                Bitmap frameAtTime = retriever.getFrameAtTime(500 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                runOnUiThread(()->{
                    iv.setImageBitmap(frameAtTime);
                });
            }).start();
        }else {
            Toast.makeText(this,  "文件不存在" ,Toast.LENGTH_SHORT).show();
        }

        Button button  = findViewById(R.id.getVideoPic);
        button.setOnClickListener(v -> {
            new Thread(()->{
                Bitmap frameAtTime1 = retriever.getFrameAtTime(1500 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                runOnUiThread(()->{
                    iv.setImageBitmap(frameAtTime1);
                });
            }).start();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            retriever.release();
        } catch (IOException e) {
            Log.e("CutVideoActivity", "onDestroy: retriever.release()异常" );
        }
    }
}